home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Dr. Windows 3
/
dr win3.zip
/
dr win3
/
PROGRAMR
/
OLE2BOOK.ZIP
/
CHAP13.ZIP
/
PATRON
/
PATRON.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1993-06-27
|
17KB
|
714 lines
/*
* PATRON.CPP
* Modifications for Chapter 13
*
* WinMain which is all we need for the basic application.
*
* Copyright (c)1993 Microsoft Corporation, All Rights Reserved
*
* Kraig Brockschmidt, Software Design Engineer
* Microsoft Systems Developer Relations
*
* Internet : kraigb@microsoft.com
* Compuserve: >INTERNET:kraigb@microsoft.com
*/
#define INITGUIDS
#include "patron.h"
//CHAPTER13MOD
//Count number of objects and number of locks.
ULONG g_cObj=0;
ULONG g_cLock=0;
//Make window handle global so other code can cause a shutdown
HWND g_hWnd=NULL;
//Indicate if the user has control
BOOL g_fUser=TRUE;
//End CHAPTER13MOD
/*
* WinMain
*
* Purpose:
* Main entry point of application. Should register the app class
* if a previous instance has not done so and do any other one-time
* initializations.
*/
int PASCAL WinMain (HINSTANCE hInst, HINSTANCE hPrev
, LPSTR pszCmdLine, int nCmdShow)
{
LPCPatronFrame pFR;
FRAMEINIT fi;
WPARAM wRet;
#ifndef WIN32
SetMessageQueue(96);
#endif
//Attempt to allocate and initialize the application
pFR=new CPatronFrame(hInst, hPrev, pszCmdLine, nCmdShow);
fi.idsMin=IDS_FRAMEMIN;
fi.idsMax=IDS_FRAMEMAX;
fi.idsStatMin=IDS_STATMESSAGEMIN;
fi.idsStatMax=IDS_STATMESSAGEMAX;
fi.idStatMenuMin=ID_MENUFILE;
fi.idStatMenuMax=ID_MENUHELP;
fi.iPosWindowMenu=WINDOW_MENU;
fi.cMenus=CMENUS;
//If we can initialize pFR, start chugging messages
if (pFR->FInit(&fi))
wRet=pFR->MessageLoop();
delete pFR;
return wRet;
}
//CHAPTER13MOD
/*
* ObjectDestroyed
*
* Purpose:
* Function for the Patron Document object to call when it gets destroyed.
* We destroy the main window if the proper conditions are met for
* shutdown.
*
* Parameters:
* None
*
* Return Value:
* None
*/
void FAR PASCAL ObjectDestroyed(void)
{
g_cObj--;
//No more objects, no locks, no user control, shut the app down.
if (0==g_cObj && 0==g_cLock && IsWindow(g_hWnd) && !g_fUser)
PostMessage(g_hWnd, WM_CLOSE, 0, 0L);
return;
}
//End CHAPTER13MOD
/*
* CPatronFrame::CPatronFrame
* CPatronFrame::~CPatronFrame
*
* Constructor Parameters:
* hInst HINSTANCE from WinMain
* hInstPrev HINSTANCE from WinMain
* pszCmdLine LPSTR from WinMain
* nCmdShow int from WInMain
*/
CPatronFrame::CPatronFrame(HINSTANCE hInst, HINSTANCE hInstPrev
, LPSTR pszCmdLine, int nCmdShow)
: CFrame(hInst, hInstPrev, pszCmdLine, nCmdShow)
{
//CHAPTER13MOD
char szTemp[256];
m_fInitialized=FALSE;
m_fEmbedding=FALSE; //-Embedding on command line?
ParseCmdLine(m_pszCmdLine, &m_fEmbedding, szTemp);
g_fUser=!m_fEmbedding;
m_dwRegCO=0;
m_pIClassFactory=NULL;
//End CHAPTER13MOD
return;
}
CPatronFrame::~CPatronFrame(void)
{
//CHAPTER13MOD
//Opposite of CoRegisterClassObject, takes class factory ref to 1
if (0L!=m_dwRegCO)
CoRevokeClassObject(m_dwRegCO);
//This should be the last ::Release, which frees the class factory.
if (NULL!=m_pIClassFactory)
m_pIClassFactory->Release();
//End CHAPTER13MOD
OleFlushClipboard();
if (m_fInitialized)
OleUninitialize();
return;
}
/*
* CPatronFrame::FInit
*
* Purpose:
* Call OleInitialize then calling down into the base class
* initialization.
*
* Parameters:
* pFI LPFRAMEINIT containing initialization parameters.
*
* Return Value:
* BOOL TRUE if initialization succeeded, FALSE otherwise.
*/
BOOL CPatronFrame::FInit(LPFRAMEINIT pFI)
{
DWORD dwVer;
dwVer=OleBuildVersion();
if (rmm!=HIWORD(dwVer))
return FALSE;
if (FAILED(OleInitialize(NULL)))
return FALSE;
m_fInitialized=TRUE;
//CHAPTER13MOD
if (m_fEmbedding)
{
HRESULT hr;
m_pIClassFactory=new CLinkClassFactory(this);
if (NULL==m_pIClassFactory)
return FALSE;
//Since we hold on to this, we should AddRef it.
m_pIClassFactory->AddRef();
hr=CoRegisterClassObject(CLSID_PatronPages, (LPUNKNOWN)m_pIClassFactory
, CLSCTX_LOCAL_SERVER, REGCLS_SINGLEUSE, &m_dwRegCO);
if (FAILED(hr))
return FALSE;
}
//End CHAPTER13MOD
return CFrame::FInit(pFI);
}
/*
* CPatronFrame::CreateCClient
*
* Purpose:
* Constructs a new client specific to the application.
*
* Parameters:
* None
*
* Return Value:
* LPCClient Pointer to the new client object.
*/
LPCClient CPatronFrame::CreateCClient(void)
{
return (LPCClient)(new CPatronClient(m_hInst));
}
/*
* CPatronFrame::FRegisterAllClasses
*
* Purpose:
* Registers all classes used in this application.
*
* Parameters:
* None
*
* Return Value:
* BOOL TRUE if registration succeeded, FALSE otherwise.
*/
BOOL CPatronFrame::FRegisterAllClasses(void)
{
WNDCLASS wc;
//First let the standard frame do its thing
if (!CFrame::FRegisterAllClasses())
return FALSE;
//We need double-clicks now and for object activation later.
wc.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
wc.hInstance = m_hInst;
wc.cbClsExtra = 0;
wc.lpfnWndProc = PagesWndProc;
wc.cbWndExtra = CBPAGESWNDEXTRA;
wc.hIcon = NULL;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_APPWORKSPACE+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = SZCLASSPAGES;
if (!RegisterClass(&wc))
return FALSE;
return TRUE;
}
//CHAPTER13MOD
/*
* CPatronFrame::FPreShowInit
*
* Purpose:
* Called from FInit before intially showing the window. We do whatever
* else we want here, modifying m_nCmdShow as necessary which affects
* ShowWindow in FInit.
*
* Parameters:
* None
*
* Return Value:
* BOOL TRUE if this initialization succeeded, FALSE otherwise.
*/
BOOL CPatronFrame::FPreShowInit(void)
{
//Base class does nothing
CFrame::FPreShowInit();
//Save the window handle for shutdown if necessary.
g_hWnd=m_hWnd;
//If we're -Embedding, don't show the window initially.
if (m_fEmbedding)
m_nCmdShow=SW_HIDE;
return TRUE;
}
/*
* CPatronFrame::ParseCommandLine
*
* Purpose:
* Allows the application to parse the command line and take action
* after the window has possibly been shown. For a compound document
* server we need to just make sure that if -Embedding is there that
* we take no file action. FPreShowInit has already handled the
* window visibility.
*
* Parameters:
* None
*
* Return Value:
* BOOL TRUE if this initialization succeeded, FALSE otherwise.
*/
void CPatronFrame::ParseCommandLine(void)
{
//If -Embedding was there, prevent any attempt at loading a file.
if (m_fEmbedding)
return;
CFrame::ParseCommandLine();
return;
}
//End CHAPTER13MOD
/*
* CPatronFrame::OnCommand
*
* Purpose:
* WM_COMMAND handler for the Patron frame window that processes extra
* File menu items as well as the Page menu.
*
* Parameters:
* hWnd HWND of the frame window.
* wParam WPARAM of the message.
* lParam LPARAM of the message.
*
* Return Value:
* LRESULT Return value for the message.
*/
LRESULT CPatronFrame::OnCommand(HWND hWnd, WPARAM wParam, LPARAM lParam)
{
LPCPatronDoc pDoc;
COMMANDPARAMS(wID, wCode, hWndMsg);
/*
* Don't bother with anything during first initialization,
* skipping many GizmoBar notifications.
*/
if (m_fI